const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return `${[year, month, day].map(formatNumber).join('/')} ${[hour, minute, second].map(formatNumber).join(':')}`
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : `0${n}`
}

function inArray(arr, key ,val){
  for (let i = 0; i < arr.length; i++) {
      if (arr[i][key] === val) {
          return i
      }
  }
  return -1
}


function utf8ByteToUnicodeStr(utf8Bytes) {
  var unicodeStr = "";
  for (var pos = 0; pos < utf8Bytes.length;) {
    var flag = utf8Bytes[pos];
    var unicode = 0;
    if ((flag >>> 7) === 0) {
      unicodeStr += String.fromCharCode(utf8Bytes[pos]);
      pos += 1;

    } else if ((flag & 0xFC) === 0xFC) {
      unicode = (utf8Bytes[pos] & 0x3) << 30;
      unicode |= (utf8Bytes[pos + 1] & 0x3F) << 24;
      unicode |= (utf8Bytes[pos + 2] & 0x3F) << 18;
      unicode |= (utf8Bytes[pos + 3] & 0x3F) << 12;
      unicode |= (utf8Bytes[pos + 4] & 0x3F) << 6;
      unicode |= (utf8Bytes[pos + 5] & 0x3F);
      unicodeStr += String.fromCharCode(unicode);
      pos += 6;

    } else if ((flag & 0xF8) === 0xF8) {
      unicode = (utf8Bytes[pos] & 0x7) << 24;
      unicode |= (utf8Bytes[pos + 1] & 0x3F) << 18;
      unicode |= (utf8Bytes[pos + 2] & 0x3F) << 12;
      unicode |= (utf8Bytes[pos + 3] & 0x3F) << 6;
      unicode |= (utf8Bytes[pos + 4] & 0x3F);
      unicodeStr += String.fromCharCode(unicode);
      pos += 5;

    } else if ((flag & 0xF0) === 0xF0) {
      unicode = (utf8Bytes[pos] & 0xF) << 18;
      unicode |= (utf8Bytes[pos + 1] & 0x3F) << 12;
      unicode |= (utf8Bytes[pos + 2] & 0x3F) << 6;
      unicode |= (utf8Bytes[pos + 3] & 0x3F);
      unicodeStr += String.fromCharCode(unicode);
      pos += 4;

    } else if ((flag & 0xE0) === 0xE0) {
      unicode = (utf8Bytes[pos] & 0x1F) << 12;
      ;
      unicode |= (utf8Bytes[pos + 1] & 0x3F) << 6;
      unicode |= (utf8Bytes[pos + 2] & 0x3F);
      unicodeStr += String.fromCharCode(unicode);
      pos += 3;
    } else if ((flag & 0xC0) === 0xC0) { //110
      unicode = (utf8Bytes[pos] & 0x3F) << 6;
      unicode |= (utf8Bytes[pos + 1] & 0x3F);
      unicodeStr += String.fromCharCode(unicode);
      pos += 2;
    } else {
      unicodeStr += String.fromCharCode(utf8Bytes[pos]);
      pos += 1;
    }
  }
  return unicodeStr;
}

// ArrayBuffer转16进度字符串hex示例
function arrayToHexString(buffer) {
  const hexArr = Array.prototype.map.call(
    new Uint8Array(buffer),
    function (bit) {
      return (`00${bit.toString(16)}`).slice(-2)
    }
  )
  return hexArr.join('')
}
//16进制字符串转Array
function hexStrToArray(hexStr) {
  var typedArray = new Uint8Array(hexStr.match(/[\da-f]{2}/gi).map(function (h) {
    return parseInt(h, 16)
  }))
  return typedArray.buffer
}
//下一包ACK指令
function sendNextAckCommand() {
  let ackStr = "fffe3000ff032c"      
  return hexStrToArray(ackStr)
}
//重发ACK
function sendReplyAckCommand() {
  let ackStr = "fffe3001ff032d"      
  return hexStrToArray(ackStr)
}
//打开实时数据上传
function openRealTime() {
  let cmdStr = "fffe3405000236"
  return hexStrToArray(cmdStr)
}
//打开历史数据上传
function openFlashData() {
  let cmdStr = "fffe3406000237"
  return hexStrToArray(cmdStr)
}
//读取设备信息
function readPathStatus() {
  let cmdStr = "fffe0f020c"
  return hexStrToArray(cmdStr)
}


module.exports = {
  formatTime,
  utf8ByteToUnicodeStr: utf8ByteToUnicodeStr,
  arrayToHexString: arrayToHexString,
  sendReplyAckCommand: sendReplyAckCommand,
  sendNextAckCommand: sendNextAckCommand,
  openRealTime: openRealTime,
  openFlashData: openFlashData,
  readPathStatus: readPathStatus,
  hexStrToArray: hexStrToArray,
  inArray: inArray,
}


